package com.zmf.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.zmf.demo.Result;
import com.zmf.demo.mapper.StudentMapper;
import com.zmf.demo.pojo.entity.Student;
import com.zmf.demo.pojo.vo.StudentVo;
import com.zmf.demo.service.StudentService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

@Service
public class ServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;
    @Override
    public Result selectbypage(Integer current, Integer page, StudentVo studentVo) {
        Page<Student> studentPage = new Page<>();
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(studentVo.getEname())){
            studentQueryWrapper.like("sname",studentVo.getEname());
        }
        if (Objects.nonNull(studentVo.getStartDate())){
            studentQueryWrapper.gt("sdate",studentVo.getStartDate());
        }
        if (Objects.nonNull(studentVo.getEndDate())){
            studentQueryWrapper.lt("sdate",studentVo.getEndDate());
        }
        IPage<Student> selectbypage = studentMapper.selectbypage(studentPage, studentQueryWrapper);

        return new Result(200,"查询成功",selectbypage);
    }




    @Override
    public Result insert(Student student) {

        int insert = studentMapper.insert(student);
        return new Result(200,"插入成功",insert);
    }

    @Override
    public Result update(Student student) {
        int i = studentMapper.updateById(student);
        return new Result(200,"更新成功",i);
    }

    @Override
    public Result delete(Integer id) {
        int i = studentMapper.deleteById(id);
        return new Result(200,"删除成功",i);
    }

    @Override
    public Result selectbyid(Integer id) {
        Student student = studentMapper.selectById(id);

        return new Result(200,"查询单个成功",student);
    }
}
